package com.lazy.mongodemo;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.client.model.geojson.Point;
import com.mongodb.client.model.geojson.Position;


public class GpsDocument {

    private static AtomicInteger seq = new AtomicInteger(0);
    
    public static Document toDoc(int flag) {
        HostCurstatus hostcur = getRandomData(flag);
        Document doc =  new Document("hostid", hostcur.getHostid())
                .append("hostno", hostcur.getHostno())
                .append("inmonitor", hostcur.getInmonitor())
                .append("monnote", hostcur.getMonnote())
                .append("posTime", hostcur.getPosTime())
                .append("posDatafmt", hostcur.getPosDatafmt())

                //.append("posLat", hostcur.getPosLat())
                //.append("posLong", hostcur.getPosLong())
                .append("loc", getPoint(hostcur.getPosLong(), hostcur.getPosLat()))

                
                .append("posSpeed", hostcur.getPosSpeed())
                .append("posAngle", hostcur.getPosAngle())
                .append("posStars", hostcur.getPosStars())

                .append("posSiglevel", hostcur.getPosSiglevel())
                .append("workmode", hostcur.getWorkmode())
                .append("curstatus", hostcur.getCurstatus())
                .append("posdataUpint", hostcur.getPosdataUpint())
                .append("powerVol", hostcur.getPowerVol())

                .append("batVol", hostcur.getBatVol())
                .append("statusUpdatetime", hostcur.getStatusUpdatetime())
                .append("warnid", hostcur.getWarnid())
                .append("gateno", hostcur.getGateno())
                .append("accstatus", hostcur.getAccstatus())
                
                .append("highBeamStatus", hostcur.getHighBeamStatus())
                .append("lowBeamStatus", hostcur.getLowBeamStatus())
                .append("leftTurnStatus", hostcur.getLeftTurnStatus())
                .append("rightTurnStatus", hostcur.getRightTurnStatus())
                .append("brakeStatus", hostcur.getBrakeStatus());
        
        
        return doc;
    }
    
    public static Point getPoint (double lon, double lat) {
        return new Point(new Position(lon, lat));
    }
    public static HostCurstatus getRandomData(int flag) {
        long prefix = 3000000;
        int sel = flag % 3;
        switch(sel){
            case 1:
                prefix = 3100000;
                break;
            case 2:
                prefix = 3200000;
                break;
            case 3:
                prefix = 3300000;
                break;
            default:
                prefix = 3000000;
                break;
        }

        long hostid =prefix + getSeq();
        // String hostNo = "苏" + getSeq();
        String hostNo = Long.toString(hostid);
        return getHostCurstatus(hostid, hostNo);
    }
    
    private static int getSeq() {
        if (seq.get() >= 100000) {
            seq.set(0);
        }
        return seq.incrementAndGet();
    }
    
    private static AtomicInteger posseq = new AtomicInteger(0);
    private static int getPosSeq() {
        if (posseq.get() >= 25962) {
            posseq.set(0);
        }
        return posseq.getAndAdd(7);
    }
    
    private static HostCurstatus getHostCurstatus(long hostId, String hostNo) {
        HostCurstatus dataTrack = new HostCurstatus();
        //gateway no
        dataTrack.setGateno((long)1);
        //host id no
        dataTrack.setHostid(Long.toString(hostId));
        dataTrack.setHostno(hostNo);
        
        //receive data  time
        Date recvDate = new Date();
        //device report time        
        dataTrack.setPosTime(recvDate);
        dataTrack.setStatusUpdatetime(recvDate);
        //vehicle speed
        long vSpeed = (long) 50;
        dataTrack.setPosSpeed(vSpeed);
        //latitude longitude angle
        float tmp = getPosSeq()/10000f;
        float longitude = 118.0f + tmp;
        tmp = getPosSeq()/10000f;
        float latitude = 32.0f + tmp;

        dataTrack.setPosLat((double)latitude);
        dataTrack.setPosLong((double)longitude);
        dataTrack.setPosAngle((long)30);
        dataTrack.setCurstatus(8L);
        
        // added later
        dataTrack.setHighBeamStatus(0);
        dataTrack.setLowBeamStatus(0);
        dataTrack.setLeftTurnStatus(0);
        dataTrack.setRightTurnStatus(0);
        dataTrack.setBrakeStatus(1);

        return dataTrack;
    }
    
    public static void main(String[] args){
        for (int i = 0; i < 10000; i++) {
            Document doc = toDoc(i);
            System.out.println(doc);
        }
        

    }
}
